package com.daluzy.chmanager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.daluzy.chmanager.MyApplication;
import com.daluzy.chmanager.R;
import com.daluzy.chmanager.bean.Account;
import com.daluzy.chmanager.bean.Employee;
import com.daluzy.chmanager.bean.Manager;
import com.daluzy.chmanager.bean.Students;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class LoginActivity extends AppCompatActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.radio_student)
    RadioButton radioStudent;
    @BindView(R.id.radio_employee)
    RadioButton radioEmployee;
    @BindView(R.id.radio_manager)
    RadioButton radioManager;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.ll_edit)
    LinearLayout llEdit;
    private Context context;
    private LoadingDialog loadingDialog;
    private int loginType = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_daluzy);
        ButterKnife.bind(this);
        context = this;
        loadingDialog = new LoadingDialog(this);
        radioGroup.check(R.id.radio_student);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio_student:
                        loginType = 1;
                        llEdit.setBackgroundResource(R.drawable.student_login_bg);
                        etAccount.setText("");
                        etPassword.setText("");
                        break;
                    case R.id.radio_employee:
                        loginType = 2;
                        llEdit.setBackgroundResource(R.drawable.employee_login_bg);
                        etAccount.setText("");
                        etPassword.setText("");
                        break;
                    case R.id.radio_manager:
                        loginType = 3;
                        llEdit.setBackgroundResource(R.drawable.manager_login_bg);
                        etAccount.setText("");
                        etPassword.setText("");
                        break;
                }
            }
        });

    }

    @OnClick({R.id.btn_login, R.id.tv_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                checkEmpty();
                break;
            case R.id.tv_register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;

        }
    }

    private void checkEmpty() {
        if (loadingDialog != null)
            loadingDialog.show();
        String account = etAccount.getText().toString().trim();
        String password = etPassword.getText().toString().trim();

        if (TextUtils.isEmpty(account)) {
            Toast.makeText(context, "请输入账号", Toast.LENGTH_SHORT).show();
            if (loadingDialog != null)
                loadingDialog.close();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(context, "请输入登录密码", Toast.LENGTH_SHORT).show();
            if (loadingDialog != null)
                loadingDialog.close();
            return;
        }
        login(account, password);
    }

    private void login(String account, final String password) {
        BmobQuery<Account> accountBmobQuery = new BmobQuery<Account>();
        accountBmobQuery.addWhereEqualTo("account", account);
        accountBmobQuery.findObjects(new FindListener<Account>() {
            @Override
            public void done(List<Account> object, BmobException e) {
                if (e == null) {
                    if (object != null && object.size() > 0) {
                        Account account1 = object.get(0);
                        if (account1.getPassword().equals(password)) {
                            if (loginType == 1) {
                                checkStudent(account1.getAccount());
                            } else if (loginType == 2) {
                                checkEmployee(account1.getAccount());
                            } else if (loginType == 3) {
                                checkManager(account1.getAccount());
                            }

                        } else {
                            Toast.makeText(context, "账号密码错误", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(context, "账号不存在", Toast.LENGTH_SHORT).show();
                    }
                    if (loadingDialog != null)
                        loadingDialog.close();
                } else {
                    if (TextUtils.isEmpty(e.getMessage())) {
                        Toast.makeText(context, "账号不存在", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    if (loadingDialog != null)
                        loadingDialog.close();
                }

            }
        });

    }

    private void checkStudent(String account) {
        BmobQuery<Students> accountBmobQuery = new BmobQuery<Students>();
        accountBmobQuery.addWhereEqualTo("account", account);
        accountBmobQuery.findObjects(new FindListener<Students>() {
            @Override
            public void done(List<Students> object, BmobException e) {
                if (e == null) {
                    if (object != null && object.size() > 0) {
                        Students students = object.get(0);
                        if (students != null) {
                            MyApplication.getIntance().setUserType(1);
                            MyApplication.getIntance().setStudents(students);
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.putExtra("type", 1);
                            startActivity(intent);
                            Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(context, "该账号不是学生账号", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(context, "该账号不是学生账号", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (TextUtils.isEmpty(e.getMessage())) {
                        Toast.makeText(context, "该账号不是学生账号", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                if (loadingDialog != null)
                    loadingDialog.close();
            }
        });

    }

    private void checkEmployee(String account) {
        BmobQuery<Employee> accountBmobQuery = new BmobQuery<Employee>();
        accountBmobQuery.addWhereEqualTo("account", account);
        accountBmobQuery.findObjects(new FindListener<Employee>() {
            @Override
            public void done(List<Employee> object, BmobException e) {
                if (e == null) {
                    if (object != null && object.size() > 0) {
                        Employee employee = object.get(0);
                        if (employee != null) {
                            MyApplication.getIntance().setUserType(2);
                            MyApplication.getIntance().setEmployee(employee);
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.putExtra("type", 2);
                            startActivity(intent);
                            Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(context, "该账号不是后勤人员账号", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(context, "该账号不是后勤人员账号", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    if (TextUtils.isEmpty(e.getMessage())) {
                        Toast.makeText(context, "该账号不是后勤人员账号", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                if (loadingDialog != null)
                    loadingDialog.close();
            }
        });

    }

    private void checkManager(String account) {
        BmobQuery<Manager> accountBmobQuery = new BmobQuery<Manager>();
        accountBmobQuery.addWhereEqualTo("account", account);
        accountBmobQuery.findObjects(new FindListener<Manager>() {
            @Override
            public void done(List<Manager> object, BmobException e) {
                if (e == null) {
                    if (object != null && object.size()>0){
                        Manager manager = object.get(0);
                        if (manager != null) {
                            MyApplication.getIntance().setUserType(3);
                            MyApplication.getIntance().setManager(manager);
                            Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.putExtra("type", 3);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(context, "该账号不是管理员账号", Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(context, "该账号不是管理员账号", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    if (TextUtils.isEmpty(e.getMessage())) {
                        Toast.makeText(context, "该账号不是管理员账号", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                if (loadingDialog != null)
                    loadingDialog.close();
            }
        });

    }

}
